package com.neegix.log.operationLog.infrastructure.repository.impl;

import com.neegix.application.query.NebulaSQL;
import com.neegix.base.PageVO;
import com.neegix.log.operationLog.application.assembler.OperationLogAssembler;
import com.neegix.log.operationLog.application.cqrs.query.OperationLogQueryRepository;
import com.neegix.log.operationLog.application.cqrs.query.condition.OperationLogWhereGroup;
import com.neegix.log.operationLog.application.dto.OperationLogDTO;
import com.neegix.log.operationLog.interfaces.vo.OperationLogVO;
import com.neegix.log.operationLog.infrastructure.repository.convert.OperationLogConverter;
import com.neegix.log.operationLog.infrastructure.repository.dataobject.OperationLogDO;
import com.neegix.log.operationLog.infrastructure.repository.mapper.OperationLogMapper;
import com.neegix.log.operationLog.infrastructure.repository.mapper.customized.OperationLogCustomizedMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

 /**
  * This file is auto-generated by nebula-framework-generator.
  * The auto-generation plugin was developed using IntelliJ IDEA Community Edition.
  * Thanks to JetBrains for their outstanding contributions to the developer community.
  * <p>
  * The code generated by this tool is owned by the user of the tool.
  * The tool itself is copyrighted by <a href="https://www.neegix.com">https://www.neegix.com</a>.
  *
  * @author <a href="https://www.neegix.com">https://www.neegix.com</a>
  * @version 1.0.0
  * @since 2024-11-24 19:51:10
  */

@Repository
public class OperationLogQueryRepositoryImpl implements OperationLogQueryRepository {

    @Autowired
    private OperationLogMapper operationLogMapper;

    @Autowired
    private OperationLogCustomizedMapper operationLogCustomizedMapper;

    @Override
    public Optional<OperationLogVO> findById(Long id) {
        OperationLogDO operationLogDO = operationLogMapper.selectByPrimaryKey(id);
        return Optional.ofNullable(OperationLogAssembler.INSTANCE.covertVO(OperationLogConverter.INSTANCE.covertDTO(operationLogDO)));
    }

    @Override
    public Integer selectCount(List<Long> ids) {
        return operationLogCustomizedMapper.selectCountByIds(ids);
    }

    @Override
    public PageVO<OperationLogVO> findPage(Integer currentPage, Integer pageSize, OperationLogDTO operationLogDTO) {
        NebulaSQL nebulaSQL = new NebulaSQL();
        nebulaSQL.createWhereGroups(OperationLogWhereGroup.class)
           .andCreateTimeBetween(operationLogDTO.getStartCreateTime(),operationLogDTO.getEndCreateTime())
           .andCreateUserEqualTo(operationLogDTO.getCreateUser())
           .andDescriptionLikeTo(operationLogDTO.getDescription())
           .andIdEqualTo(operationLogDTO.getId())
           .andModuleLikeTo(operationLogDTO.getModule())
           .andTypeEqualTo(operationLogDTO.getType())
           .andUpdateTimeBetween(operationLogDTO.getStartUpdateTime(),operationLogDTO.getEndUpdateTime())
           .andUpdateUserEqualTo(operationLogDTO.getUpdateUser());
           
        nebulaSQL.setPager(currentPage, pageSize);
        List<OperationLogDO> result = operationLogMapper.selectList(nebulaSQL);
        Long total = operationLogMapper.selectCount(nebulaSQL);
        PageVO<OperationLogVO> page = new PageVO<>(currentPage, pageSize);
        page.setTotal(total);
        page.setResult(OperationLogAssembler.INSTANCE.covertVO(OperationLogConverter.INSTANCE.covertDTOS(result)));
        return page;
    }
}
